package com.wngbms.lettuce;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisStringAsyncCommands;
import io.lettuce.core.api.reactive.RedisReactiveCommands;
import io.lettuce.core.api.sync.RedisCommands;
import reactor.core.publisher.Mono;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class FirstDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

//        RedisClient client = RedisClient.create("redis://192.168.75.110");
        RedisClient client = RedisClient.create("redis://localhost");
        m1(client);
//        m2(client);
//        m3(client);



    }

    private static void m3(RedisClient client) {
        StatefulRedisConnection<String, String> connect = client.connect();
        RedisReactiveCommands<String, String> reactive = connect.reactive();
        Mono<String> set = reactive.set("reactive", "v2");
        Mono<String> get = reactive.get("reactive");

        set.subscribe();
        System.out.println(get.block());
    }

    private static void m2(RedisClient client) throws InterruptedException, ExecutionException {
        StatefulRedisConnection<String, String> connect = client.connect();
        RedisStringAsyncCommands<String, String> async = connect.async();
        RedisFuture<String> set = async.set("api", "v1");
        RedisFuture<String> get = async.get("api");

        System.out.println(set.get());
        System.out.println(get.get());
    }

    private static void m1(RedisClient client) {
        StatefulRedisConnection<String, String> connect = client.connect();
        RedisCommands<String, String> sync = connect.sync();
        String value = sync.get("k1");
        System.out.println(value);

        try {
            TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
